<template>
  <div class="wrapper">
    <div class="file-list">
      <div class="file-item" v-for="(item, index) in uploadList" :key="index">
        <img src="@/assets/common/file-icon.png" />
        <div class="file-name van-ellipsis">{{ item.name }}</div>
        <img class="del" src="@/assets/common/delete-icon.png" @click="del(index)" />
      </div>
    </div>
    <van-uploader :max-size="maxSize" @oversize="onOversize" :max-count="limit" :after-read="afterRead" v-model="fileList" :preview-image="false">
      <div class="upload-btn">
        <van-icon name="plus" color="#2A81E6" />
        <span class="text">添加附件</span>
      </div>
    </van-uploader>
  </div>
</template>

<script>
import { uploadFile } from "@/api/common";
import { Toast, Dialog } from "vant";
export default {
  props: {
    limit: {
      type: Number,
      default: 5
    },
    maxSize: {
      type: Number,
      default: 5242880
    },
    oversizeTitle: {
      type: String,
      default: "文件大小不能超过5M"
    },
    delFn: {
      type: Function
    }
  },
  data() {
    return {
      fileList: [],
      uploadList: []
    };
  },
  methods: {
    afterRead(file) {
      let formData = new FormData();
      formData.append("file", file.file);
      uploadFile(formData).then(res => {
        if (res.code == 200) {
          this.uploadList.push({
            name: this.fileList[this.fileList.length - 1].file.name,
            url: res.data.url,
            size: res.data.size
          });
          this.$emit("fileChange", this.uploadList);
        } else {
          Toast(res.msg);
        }
      });
    },
    onOversize() {
      Toast(this.oversizeTitle);
    },
    del(index) {
      Dialog.confirm({
        title: "删除",
        message: "确定删除该文件吗？"
      })
        .then(() => {
          if (this.fileList[index].fileId && this.delFn) {
            this.delFn(this.fileList[index].fileId).then(res => {
              if (res.code == 200) {
                this.fileList.splice(index, 1);
                this.uploadList.splice(index, 1);
                this.$emit("fileChange", this.uploadList);
              } else {
                Toast(res.msg);
              }
            });
          } else {
            this.fileList.splice(index, 1);
            this.uploadList.splice(index, 1);
            this.$emit("fileChange", this.uploadList);
          }
        })
        .catch(() => {});
    },
    setData(file) {
      this.fileList = JSON.parse(JSON.stringify(file));
      this.uploadList = JSON.parse(JSON.stringify(file));
    }
  }
};
</script>

<style lang="scss" scoped>
.wrapper {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  .upload-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: calc(100vw - 64px);
    height: 48px;
    border-radius: 8px;
    background: #fafbfc;
    color: #2a81e6;
    font-size: 13px;
    margin-top: 4px;
    .text {
      margin-left: 12px;
    }
  }
  .file-item {
    display: flex;
    align-items: center;
    border-radius: 8px;
    background: #fafbfc;
    margin-bottom: 12px;
    padding: 14px 16px;
    img {
      display: block;
      width: 26px;
      margin-right: 12px;
    }
    .file-name {
      color: #2a81e6;
      font-size: 13px;
      line-height: 18px;
      width: 210px;
    }
    img.del {
      width: 18px;
      margin-right: 0;
      margin-left: 12px;
    }
  }
}
</style>
